<?php

class PageView extends Mysql {
	public function __construct() {
		parent::__construct();
	}
	/**
	 * 得到所有的访问量
	 */
	public function queryTotalView() {
		$sql = "select sum(pageview) as total from pageview;";
		$rows = $this->exec_query($sql);
		
		if ($rows) {
			return $rows[0][total];
		}
		return 9999;
	}
	
	/**
	 * 得到今天的访问量
	 */
	public function queryTodayView() {
		$sql = "select pageView from pageview where datetime = CURDATE();";
		$rows = $this->exec_query($sql);
	
		if ($rows) {
			return $rows[0][pageView];
		}
		return 0;
	}
	/**
	 * 增加一个访问量
	 * 如果是今天的第一个，就添加记录，否则更新记录
	 */
	public function addOneView() {
		$this->beginTrans();
		$current_date = date("Y-m-d");
		$sql = "select id from pageview where datetime = '$current_date';";
		$rows = $this->exec_query($sql);
		if ($rows) {//有今天的值
			$sql = "update pageview set pageview = pageview + 1 where datetime = '$current_date';";
		}
		else {
			$sql = "insert into pageview(dateTime, pageView) values('$current_date', 1);";
		}
		$flag = $this->exec_update($sql);
		$this->commit();
		
		return $flag;
	}
}

?>